package com.huiminpay.merchant.controller;


import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.domain.PageVO;
import com.huiminpay.common.cache.util.QRCodeUtil;
import com.huiminpay.merchant.api.MerchantService;
import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.dto.StoreDTO;
import com.huiminpay.transaction.api.TransactionService;
import com.huiminpay.transaction.api.dto.QRCodeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Api(value = "商户平台‐门店管理", tags = "商户平台‐门店管理", description = "商户平台‐门店的增删改查")
@RestController
public class StoreController {

    @Reference
    MerchantService merchantService;
    @Reference
    TransactionService transactionService;
    @Value("${huiminpay.c2b.subject}")
    String subject;
    @Value("${huiminpay.c2b.body}")
    String body;

    @ApiOperation("分页条件查询商户下门店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int",
                    paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType =
                    "int", paramType = "query")})
    @PostMapping("/my/stores/merchants/page")
    public PageVO<StoreDTO> queryStoreByPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {

        //解析商户Id
        Long merchantId = SecurityUtil.getMerchantId();

        StoreDTO storeDTO = new StoreDTO();
        storeDTO.setMerchantId(merchantId);
        return merchantService.queryStoreByPage(storeDTO, pageNo, pageSize);
    }

    @ApiOperation("生成商户应用门店二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "商户应用id", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "storeId", value = "商户门店id", required = true, dataType = "String", paramType ="path")
    })
    @GetMapping(value = "/my/apps/{appId}/stores/{storeId}/app-store-qrcode")
    public String createCScanBStoreQRCode(@PathVariable("appId") String appId, @PathVariable("storeId")Long storeId) {
        //通过token获取商户Id
        Long merchantId = SecurityUtil.getMerchantId();
        //查商户信息
        MerchantDTO merchantDTO = merchantService.queryMerchantById(merchantId);

        QRCodeDto qrCodeDto = new QRCodeDto();
        qrCodeDto.setAppId(appId);
        qrCodeDto.setStoreId(storeId);
        qrCodeDto.setMerchantId(merchantId);
        qrCodeDto.setSubject(subject.format(subject,merchantDTO.getMerchantName())); //订单标题
        qrCodeDto.setBody(String.format(body, merchantDTO.getMerchantName()));  //订单描述

        //二维码的url
        String url = transactionService.createStoreQRCode(qrCodeDto);
        //生成二维码
        String qrCode = null;
        try {
            qrCode = QRCodeUtil.createQRCode(url,200,200);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(CommonErrorCode.E_200007);
        }
        return qrCode;
    }

    ;
}
